package com.gxc.exception;

import lombok.extern.slf4j.Slf4j;
import org.assertj.core.util.Maps;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author GongXincheng
 * @since 2019-10-12 19:35
 */
@Slf4j
@ControllerAdvice
public class ControllerExceptionHandler {

    @ExceptionHandler({UserNotExistException.class})
    @ResponseBody
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public Map<String, Object> handleUserNotExistException(HttpServletRequest request, UserNotExistException e) {
        System.out.println("ControllerAdvice ->requestUrl：" + request.getRequestURL());
        log.error(e.getMessage(), e);
        Map<String, Object> result = new HashMap<>( 10);
//        result.put("code", HttpStatus.BAD_REQUEST);
        result.put("time", new Date());
        result.put("message", "id = " + e.getId());
        return result;
    }

}
